<template>
  <div class="xtx-goods-page">
    <!-- 数据渲染深度达到2层及以上，需要判断是否已经取到数据，否则会渲染报错 -->
    <div class="container" v-if="goodDetail.categories">
      <!-- 面包屑 -->
      <XtxBread separator="/">
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goodDetail.categories[1].id}`">{{
          goodDetail.categories[1].name
        }}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goodDetail.categories[0].id}`">{{
          goodDetail.categories[0].name
        }}</XtxBreadItem>
        <XtxBreadItem>{{ goodDetail.name }}</XtxBreadItem>
      </XtxBread>

      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 图片预览区 -->
        <div class="media">
          <goods-image :imageList="goodDetail.mainPictures"></goods-image>
          <!-- 销量组件 -->
          <GoodsSale :goodDetail="goodDetail" />
        </div>

        <!-- 商品基本信息区 -->
        <div class="spec">
          <!-- 信息组件 -->
          <GoodsName :goodDetail="goodDetail" />
          <GoodsSku :goods="goodDetail" @change="getSku" />

          <!-- 双向绑定变量 -->
          <!-- vue3 v-model语法糖=> :modelValue='buyNum' +  @update:modelValue='buyNum=data' -->
          <!--      v-model:test="buyNum" 语法糖具名传参=> :test='buyNum' +  @update:test='buyNum=data' -->
          <xtx-numbox :max="goodDetail.inventory" v-model="buyNum"></xtx-numbox>

          <xtx-button style="margin-top:20px" type='primary'>
              加入购物车
          </xtx-button>
        </div>
      </div>

      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
              <GoodsTab></GoodsTab>
        </div>

        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
            <goods-hot :type='1'></goods-hot>
            <goods-hot :type='2'></goods-hot>
            <goods-hot :type='3'></goods-hot>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, onMounted, provide } from 'vue'
import { useRoute } from 'vue-router'
import { findGoods } from '@/api/goods'
import GoodsImage from './components/GoodsImage'
import GoodsSale from './components/GoodsSale'
import GoodsName from './components/GoodsName'
import GoodsTab from './components/GoodsTab'
import GoodsHot from './components/GoodsHot'
export default {
  name: 'XtxGoodsPage',

  components: {
    GoodsImage,
    GoodsSale,
    GoodsName,
    GoodsTab,
    GoodsHot
  },

  // vue2依赖注入
  data () {
    return {
      obj: {
        key: 10
      }
    }
  },
  provide () {
    return {
      test: this.obj
    }
  },

  setup () {
    // 定义购买数量
    const buyNum = ref(1)

    const goodDetail = ref({})
    const route = useRoute()

    // 注入数据
    provide('goods', goodDetail)

    const getGoodDetail = async () => {
      const { result } = await findGoods(route.params.id)
      goodDetail.value = result
    }

    const getSku = (sku) => {
      //   console.log(sku)
      if (sku.skuId) {
        goodDetail.value.price = sku.price
        goodDetail.value.oldPrice = sku.oldPrice
        goodDetail.value.inventory = sku.inventory
      }
    }

    onMounted(() => {
      getGoodDetail()
    })

    return { goodDetail, getSku, buyNum }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;

  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
    background: #fff;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
